package com.project;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;

public class MainTomCat {
    private static ApplicationContext springContext=new AnnotationConfigApplicationContext(ServletConfig.class);

    public MainTomCat() {
        Tomcat tomcat=new Tomcat();
        //设置启动窗口
        tomcat.setPort(8087);
        //设置http引擎
        tomcat.getConnector();
        //定义上下文
        Context context=tomcat.addContext("",null);
        //注册servlet
        Wrapper w=Tomcat.addServlet(context,"findAll",(Servlet) springContext.getBean("findAll"));
        //注册servlet访问路径
        w.addMapping("/abc/*");
        try {
            //启动tomcat
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MainTomCat();
    }
}
